package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import queries.DBConnector;
import queries.ReportQueries;
import queries.SecurityQueries;
import queries.UserManagementQueries;
import entities.DiagnosedPerson;
import entities.User;

/**
 * Servlet implementation class Reports
 */
@WebServlet("/Reports")
public class Reports extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Reports() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String> messages = new HashMap<String, String>();
		request.setAttribute("messages", messages);
		
		DBConnector dbConnect = new DBConnector();
		SecurityQueries security = null;
		
		try {
			String diagnosis = (request.getParameter("diagnosis")).trim();
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
			java.util.Date fromDate = df.parse((request.getParameter("fromDate")).trim());
			java.util.Date toDate = df.parse((request.getParameter("toDate")).trim());
			
			
			security = new SecurityQueries(dbConnect.getConnection());
			ReportQueries rq = new ReportQueries(dbConnect.getConnection(), security);
			ArrayList<DiagnosedPerson> patients = rq.generateReport(diagnosis, fromDate, toDate);	
			
			if(patients.size() > 0)
				request.setAttribute("patients", patients);
			else {
				messages.put("report_msg", "<span class=\"message info\">There are no reports matching this query.</span>");
			}
			request.getRequestDispatcher("reports.jsp").include(request, response);
			
		} catch (ParseException e) {
			messages.put("report_msg", "<span class=\"message error\">Invalid date.</span>");
			request.getRequestDispatcher("reports.jsp").forward(request, response);
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
